/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Model;

import Tools.ArrayUtils;
import java.lang.reflect.InvocationTargetException;

/**
 *
 * @author Daniel
 */
public class Dialog {

    private String _text;
    private Dialog[] _answers;
    
    public Dialog() {
    }
    public Dialog(String text) {
        this._text = text;
        this._answers = new Dialog[0];//cal ferho així
    }
    public Dialog(String text, Dialog[] answers) {
        this._text = text;
        this._answers = answers;
    }
 
    public void set_text(String text) {
        this._text = text;
    }

    public void set_answers(Dialog [] answers) {
        this._answers = answers;
    }

    /***
     * @brief This function allow to add a string to our string array
     * 
     *  But I have some things in mind:
     * 
     *  First off, I m not sure about the complexity so I dont know what's the
     *  best form to expand the size. Actually, it's expanded in one unit, it means,
     *  an array of 4 is expaded to 5. 
     * 
     *  So I don't know how it's made the copy and I can't say the complexity and then
     *  I don't know if it's better to expands for instance 50% or continue with
     *  the actual comportement.
     * 
     * @param answer 
     * 
     * @return answer with lenght = (lenght + 1)
     */
    public void add_answer(Dialog answer) {
        _answers = (Dialog[]) ArrayUtils.expand(_answers,1);
        _answers[_answers.length-1] = answer;
    }
    
    public void add_answer(Dialog [] answer) {
        int auxSize = _answers.length;
        _answers = (Dialog[]) ArrayUtils.expand(_answers,answer.length);
        for(int i=auxSize;i<=_answers.length - 1;i++)
            _answers[i] = answer[i - auxSize];
    }

    public String get_text() {
        return _text;
    }

    public Dialog[] get_answers() {
        return _answers;
    }

    //testing:
    public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{
        Dialog a = new Dialog("Bon dia, que voldra realitzar?");
        Dialog a1 = new Dialog("Comprar");
        Dialog a2 = new Dialog("Vendre");
        Dialog[] a11 = {new Dialog("Sword"),new Dialog("Blade")};
        Dialog a21 = new Dialog("No tens res");
        
        a1.add_answer(a11);//add array []
        a2.add_answer(a21);
        

        a.add_answer(a1);
        a.add_answer(a2);

        //respostes del primer node
        System.out.println(a.get_text());
        new ArrayUtils().printArrayAttribute(a.get_answers(), "get_text");
        System.out.println();
        //***********************************
        
        //arbre del dialag
        System.out.println(a.get_text());
        for(Dialog aux1 : a.get_answers()){
            System.out.println("\t" + aux1.get_text());
            for(Dialog aux2 : aux1.get_answers())
                System.out.println("\t\t" + aux2.get_text());
        }
    }    
}
